<?php


namespace app\crontab\controller\once;

use app\common\lib\RedisKey;
use custom\Curl;
use custom\Mongo;
use app\crontab\model\House;
use custom\Log;
use loan\Mortgage;
use loan\RateCount;
use custom\Redis;
use map\BaiDu;
use push\Aurora;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use weChat\OfficialAccount;

class Test extends Command {

    protected function configure() {
        $this->setName('test');
    }

    protected function execute(Input $input, Output $output) {
//        $this->infoToArea();
        // $this->tb_house();
//        $this->wechatData();
        $this->curlTest();
    }

    public function curlTest() {
        $ret = $this->each('http://apis.map.qq.com/ws/coord/v1/translate?locations=36.052829,103.823858&type=3&key=ENWBZ-G2GWJ-2BUFP-K5PG5-X3O4T-5OBBO');
        print_r($ret);
    }


    protected function each($url) {
        $redis = Redis::getInstance();
        $curl = Curl::getInstance();
        $ips = $redis->get('proxy_ips');
        if (!$ips) {
            $arr = json_decode($curl->xhr("GET", 'http://d.jghttp.golangapi.com/getip?num=1&type=2&pro=0&city=0&yys=0&port=11&pack=32327&ts=0&ys=0&cs=0&lb=1&sb=0&pb=4&mr=1&regions='), true);
            if (!isset($arr['data']) || empty($arr['data'])) Log::write('获取代理ip失败', "getProxy_err.log");
            $ips = $arr['data'];
        } else {
            $ips = json_decode($ips, true);
        }
        while (1):
            $curl->setProxy($ips[0]['ip'], (string)$ips[0]['port']);
            $ret = $curl->xhr("GET", $url);
            //无效代理ip
            if (!$ret) {
                $curl->setProxy('', '');
                $arr2 = json_decode($curl->xhr("GET", 'http://d.jghttp.golangapi.com/getip?num=1&type=2&pro=0&city=0&yys=0&port=11&pack=32327&ts=0&ys=0&cs=0&lb=1&sb=0&pb=4&mr=1&regions='), true);
                if (!isset($arr2['data']) || empty($arr2['data'])) Log::write('获取代理ip失败', "getProxy_err.log");
                $ips = $arr2['data'];
            }else{
                break;
            }
        endwhile;
        if (!$redis->setex('proxy_ips', 3600, json_encode($ips))) Log::write('缓存代理ip失败', "getProxy_err.log");
        return $ret;
    }

    protected function wechatData() {
        var_dump(1111);
        $iv = base64_decode("kT+uMuPwUk2LH4cFbK0GiA==");
        $key = pack("H*", "6476b3f5ec6dcaddb637e9c9654aa698");
        $encData = "bBslLmCv9cphAg+obJ+cYG502vf5g4iUY5UN4cuaJRZw9jFb7XVW7puTGMODFUlDW1+bPBWQvcIfbt4MVyE27NyHYMgohWYDvm9l5rlik8yFlSS2xO6w1gEzFNoho49FK+zNNw3pXO4B4DOXjjSIKYSxhJ29qmrdcVty0tu9bNJ4f/0aDSJbJQUgA7ZlVX4dMl2UY47o9nyf3eEA+6TKiFQe3afE66J5C4MaR3ahVDEHtjuFzoyKvAmfd7j9C5HVZW1H6Utxy2yWnNtfPYAzQLtS5uT1xw/S+lpT5yC4h1Uj2hY6nzs2k0+NkZJ41mxhy64KW3L2JPgGukrJIvmkyif05Rgj9QcgpZHuZW/gLR+doTCVOgjtU7VtpivHJANNKLHpn9Rdpf+xyQlAg2/+DJwYOMjScLp2I0Hw6GflGsvDdDjix7KBy9lu4jyfAnwpSAdv3NUqsNH8E/M8qNbnfMPrLiGGU/Z28Yzeit2dbZLoLp+skANrXoPL9c8YnxgHISSy0XDrkeyXDcc0OjC3NMSPlsFoIMjqddzx9Ys1HyPVxAb/rvn4Vj/QzMi75pEq12Sza99s2R74jU1h3qBDRq3FU1ZBCXrxBQb79D64yVdg+6wWG4nhVlqcKA0X0KrM1QXQJXUfwgqv8gqIhvp/hh9TIR/5r7xnz3hLAS16AhD/iWCJkvk1Wum67rRL6YWa5xKnh9od+3C/UsWnStO9kYlizFs+j4bVrurjMLwc7pslPb6TtXwKgblNGcmzLOAZUk194h1EBskwrSe2YE/HvElFp0Uh+NoDG376XveeIPHm3UiJPEE8aIrKE/yqx9g18zqA+hdaHU4V+7rdTfn2eOOqprcsfLsxKX9tjP7a8PrTUkEYK0WYuNcVnGKWlEiwqrMXWvObsuNir80YDjHSl2CfJMzFizVTDQpXP+aApFs9GlVtmDVjMNZiDD2gxCZ1B8g2nRqVktvroSpj2OWCeFRALR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cK2O/xuKHbjZej5B9+daQR0y1pSPHF9evr6/c7keyqgKTYAQ9p9ptEaeQqK31U4Sci2c2ihNT1K4sVC6PihpU+2ukBLb97m2a6OG06+G8oiYVHwkn9HeSuiGpyjDm5ttt8S0Fk2O8EDCtdfwIGRVVkXuVD9QxBkOIkb2gC1L5iUy3avLrsDRr/NKhj+0SsvK4zt1j39oIHaSMinhktq6acmeU6hUDLLhdD0KUtNCElMfXP9LC1yMah0cv4DT1t4lwQPbbDJz9nL5onR5qiuK+xsPXMFdUi3dy03xCnxYZHlWcdSUImXPeGTNFE+xajBYKqwBMd/iY90bkT9z70dZuslICL45QhnNqqSu9OY3Kvwpj2/duKcLFYo3WQPAA6TL/uI5neLSoPlJm1ct6pPE5Zl0g91sD8hke2tkea4b4entW1HSS3S1zSdREz0lvN+fsB0ucwdtt9HZUfTsLhkg3x38QDi+nm5dOJPq8kdkZn1GoqvlrbyxdE0BmKx7jOrg4qAdHgWzFj1FMVZ8VcF13Qk3hbs3RhpMfUsJVP+PhQ3QaGP5IzAir62zEeg9As+ssXsYHMpr8ygNmZeGUea5U1ocnkyUhfX5lfnnDODRgJJAASPgm4t9XYv1gxOtnkxUu2YTtuz2MZlB0x1lf3s6cJrxO0cQJjut7cG6RvjvVqql5NZvzuRadAMw/s7WlyXsb6MJMo3INkirfaVPpXHf1Qs9/rJxXFLTq3M8+PIpetqR0aNnWguCrkRlmRUIK8KeA8amKdxJUBkku4bPRCgOCfEFlYPSAfqxl5/r2D6t6TIcEZbmHeigLspWMm7AcGsDd666VkNeW9RNNbZJHjs9wFFn/oMYIh5vKwdaNt30XDH1QC10fxocCKoMV6MxkCto1XPVpCpI2xlJxfZmfsf9MyXmiFISkIY+SDf27SD5N1iE2Rw==";
        $encData = base64_decode($encData);
        $data = openssl_decrypt($encData, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
        print_r($data);
    }

    /**
     * 房源详情转换的省市区
     * @return [type] [description]
     */
    protected function infoToArea() {
        $tableName = 'house_jd';
        $mongo = Mongo::getInstance();
        $cursor = $mongo->select($tableName, [], []);

        $areaArr = [];
        foreach ($cursor as $key => $value) {
            $value = (array)$value;
            $cityId = md5($value['province'] . $value['city']);

            if (!isset($arr[$cityId])) {
                $arr[$cityId] = [
                    '_id'      => $cityId,
                    'province' => $value['province'],
                    'city'     => $value['city'],
                    'itime'    => time(),
                    'area'     => [],
                ];
            }

            if (!in_array($value['area'], $arr[$cityId]['area'])) {
                $arr[$cityId]['area'][] = $value['area'];
            }


        }
        $arr = array_values($arr);
        $a = $mongo->addAll($arr, 'region_jd');
        print_r($a);
    }

    protected function bujiuHouse() {
        $data = db('house_auction')->select();
        echo count($data);
        foreach ($data as $key => $value) {
            $hData = db('house_test')->field('id,source_url')->where('id', $value['house_id'])->find();
            unset($value['id']);
            $id = db('house_auction_test')->insertGetId($value);
            if ($id == 0) {
                echo "fail {$value['source_url']} \n";
                exit;
            }
            if ($value['source_url'] == $hData['source_url']) {
                $res = db('house_test')->where('id', $hData['id'])->update(['auction_id' => $id]);
                if ($id == 0) {
                    echo "fail {$value['source_url']} \n";
                    exit;
                }
            }
        }
    }

    protected function tb_house() {
        $page = $num = 0;
        while (true) {
            $page++;
            echo $page . "\n";
            // 备份数据
            $data = db('house')->page($page, 500)->select();
            if (empty($data)) {
                echo "\n end";
                exit;
            }
            $num += db('house_test')->insertAll($data);
        }

        echo $num;
    }

    public function accessToken() {
        OfficialAccount::getAccessToken();
        $accessToken = "38_MrvkP_eNuVaUFOklticDUas3lT2R7_mVdtiao_xWEILzaWWZMHd-JFkxnvK8QD3cyEXN0BbJCq-jGPTu46xnC7xV-L1FXizge8ImHDPXPyZqaCgJmcG--TYY46nPuIpdmsO6vgnDtfrKwaOIXSKjABACXP";
    }

    public function sendMsg() {
        $accessToken = "38_MrvkP_eNuVaUFOklticDUas3lT2R7_mVdtiao_xWEILzaWWZMHd-JFkxnvK8QD3cyEXN0BbJCq-jGPTu46xnC7xV-L1FXizge8ImHDPXPyZqaCgJmcG--TYY46nPuIpdmsO6vgnDtfrKwaOIXSKjABACXP";
        OfficialAccount::sendTemplateMessage($accessToken);
    }

    public function handleRegion() {
//        $where = [
//            ['city_code', '=', 0],
//            ['lat', '>', 0]
//        ];
//        $data = db('houseAuction')->where('type',4)->field('id,initial_price')->select();
//        foreach($data as $v) {
//            db('houseAuction')->where('id',$v['id'])->update(['pay_selloff' => $v['initial_price']]);
//        }

        $data = db('house')->where('city_codes', '')->field('id,city_code')->select();
        foreach ($data as $val) {
            if (!empty($val['city_code'])) {
                $p_codes = db('region')->where('city_code', $val['city_code'])->value('p_codes');
                if (!empty($p_codes)) {
                    db('house')->where('id', $val['id'])->update(['city_codes' => "{$p_codes}{$val['city_code']},"]);
                }
            }
        }
    }

    public function houseLocation() {
        $houseModel = new House;
        $datas = db('house')->where('city_codes', '')->field('id,address,province,city,area,title')->select();
        foreach ($datas as $params) {
            $params['lat'] = $params['lng'] = 0;
//            $address = !empty($params['address']) ? "{$params['province']}{$params['city']}{$params['area']}{$params['address']}" : $params['title'];
            $address = $params['title'];
            $data = $houseModel->getAddressInfoByMap($address, $params['lat'], $params['lng']);
            // 城市code集合
            if (isset($data['city_code'])) {
                if (!$pCodes = db('region')->where(['city_code' => $data['city_code']])->value('p_codes')) {
                    $data['city_code'] = 0;
                } else {
                    $data['city_codes'] = "{$pCodes}{$data['city_code']},";
                }


            }
            $params['address'] = $params['address'] ?? ($data['map_address'] ?? '');
            $params = array_merge($params, $data);
//            print_r($params);
            $houseModel->customName('erp_house')->customUpdate($params, "id = {$params['id']}");
        }
    }

    public function handleDiscount() {
        $data = db('houseAuction')->field('id,initial_price,consult_price')->select();
        foreach ($data as $v) {
            $uData = [
                'discount' => -1
            ];
            // 评估价存在，且评估价!=初始价格时
            if (!empty($v['consult_price']) && $v['consult_price'] != $v['initial_price']) {
                $dis = round($v['initial_price'] / $v['consult_price'] * 10, 2);
                $dis < 1 && $dis = 0;
                $uData = [
                    'discount' => $dis
                ];
            }
            db('houseAuction')->where('id', $v['id'])->update($uData);
        }
        echo "count:" . count($data);
    }

}
